var FindReplay = app.controller('FindReplay', function($http, $scope, $rootScope, $location,$cordovaDevice,$cordovaChat,FindReplayData) {
	try{
		$rootScope.tab=2;
		$rootScope.showf=true;
		$scope.busy = false;
		
		$scope.currentPage = 1;
		$scope.limit = 10;
		$scope.pages = 1;
		$scope.show=true;
		$scope.busy = true;
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=freplay";
		$http.jsonp(url).success(function(data) {
			$scope.busy = false;
			if (data.done) {
			$scope.qes=data.retval.data;
			$scope.pages = Math.ceil(data.retval.total
					/ $scope.limit);
			for ( var i = 0; i < $scope.qes.length; i++) {
				if($scope.qes[i].playm==undefined||$scope.qes[i].playm==''){
					$scope.qes[i].playm="video.png";
				}
			}
			}
		});	
		
		$scope.closeAlert = function(index) {
			$scope.alerts.splice(index, 1)
		}
		
		$scope.loadMore = function() {
			if ($scope.currentPage < $scope.pages&&$scope.currentPage<=6) {
				if ($scope.busy) {
					return false;
				}
				$scope.busy = true;
				$scope.currentPage++;
				var url = SITE_URL
				+ "index.php?app=baseloginapp&act=freplay&callback=JSON_CALLBACK"+"&page="+$scope.currentPage;
				$http.jsonp(url).success(
						function(data) {
							if (data.done) {
								for ( var i in data.retval.data) {
									if (typeof data.retval.data[i] != "function")
										$scope.qes
										.push(data.retval.data[i]);
								}
								$scope.pages = Math.ceil(data.retval.total
										/ $scope.limit);
								$scope.busy = false;
								if ($scope.pages <= $scope.currentPage) {
									$scope.alerts = [ {
										type : 'success',
										msg : '没有更多内容了'
									} ]
								}
							}
						});
			}
		};
		
		
		
		//关注操作
		$scope.followtutor=function(s){
			if($scope.qes[s.$index].f==undefined||$scope.qes[s.$index].f==null||$scope.qes[s.$index].f==""||$scope.qes[s.$index].f==0){
				var id=$scope.qes[s.$index].tutor_id;
				var url=SITE_URL+"index.php?app=baseloginapp&act=followtutor&callback=JSON_CALLBACK&id="+id;
				$http.jsonp(url).success( function(data) {
					if(data.done){
						//showtoast(data.msg);
						$scope.qes[s.$index].tutorinfo.collect_num++;
						$scope.qes[s.$index].f=1;
					}
				});
			}
			
		}
		
		
		$scope.play = function(s){
			try{
				var vp=$scope.qes[s.$index].voice_path;
				if(vp!=null&&vp!=''){
					//播放音频 m 即为音频地址
					if($rootScope.media!=undefined&&$rootScope.media!=null){
						$scope.qes[s.$index].playm="video.png";
						$rootScope.media.release();
						/*
						$rootScope.media.stop();
						if(n.fromuser==$scope.m.user_id){
							$scope.queue[$scope.sindex].playm="yellow_video.png";
						}
						else{
							$scope.queue[$scope.sindex].playf="write_video.png";
						}
						console.log('replay png');
						console.log($scope.queue[$scope.sindex].playm);
						console.log($scope.queue[$scope.sindex].playf);
						$rootScope.media.src=$rootScope.site+n.msg;
						console.log('replay');
						console.log($rootScope.media.src);
						$scope.sindex=s.$index;
						if(n.fromuser==$scope.m.user_id){
							$scope.queue[$scope.sindex].playm="yellow_d.gif";
						}
						else{
							$scope.queue[$scope.sindex].playf="write_d.gif";
						}
						console.log('replay gif');
						console.log($scope.queue[$scope.sindex].playm);
						console.log($scope.queue[$scope.sindex].playf);
						$rootScope.media.play();*/
					}
					//else
					{

						$rootScope.media=audioplay($rootScope.site+vp);
						console.log('newplay');
						console.log($rootScope.media.src);
						$rootScope.media.successCallback=function(){
							$scope.qes[s.$index].playm="video.png";
							console.log('successCallback png');
							console.log($scope.qes[s.$index].playm);
							$scope.$apply();
						};
						$rootScope.media.errorCallback=function(err){
							$scope.qes[s.$index].playm="video.png";
							console.log('successCallback png');
							console.log($scope.qes[s.$index].playm);
							$scope.$apply();
						};

						$scope.qes[s.$index].playm="write_d.gif";
						console.log('newplay png');
						console.log($scope.qes[s.$index].playm);
						$rootScope.media.play();
					}
				}
			}
			catch(e){
				console.log('play');
				console.log(e);
			}
		}
	}
	catch (e) {
		showcatch(e);
	}
});
FindReplay.FindReplayData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}